package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.pojos.ApRead;
import com.heima.behavior.mapper.ApReadMapper;
import com.heima.behavior.service.IApReadService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.utils.common.UserThreadLocal;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 文章阅读表 服务实现类
 * </p>
 *
 * @author fkn
 * @since 2024-08-21
 */
@Service
public class ApReadServiceImpl extends ServiceImpl<ApReadMapper, ApRead> implements IApReadService {

    @Override
    public ResponseResult read(ApRead apRead) {
        LambdaQueryWrapper<ApRead> condition = new LambdaQueryWrapper<ApRead>()
                .eq(ApRead::getUserId, apRead.getUserId())
                .eq(ApRead::getArticleId, apRead.getArticleId());
        ApRead one = getOne(condition);
        if (one == null){
            apRead.setReadCount(apRead.getReadCount());
            save(apRead);
        }else {
            apRead.setReadCount(one.getReadCount() + 1);
            update(apRead,condition);
        }
        return ResponseResult.okResult(null);
    }
}
